class EventEmitter {

    _events?:any;
    constructor(props?:any){
        this._events = {};
    }

    dispatch = (event:string, data?:any) => {
        if(!this._events[event]) return;
        this._events[event].forEach((callback:any) => {
            callback(data);
        });
    }

    subscribe = (event:string,callback:(data:any)=>void)=>{
        if(!this._events[event]){
            this._events[event] = []
        }
        this._events[event].push(callback);
    }

    unsubscribe = (event:string)=>{
        //react app 是spa不考虑多个页面同时订阅的情况
        if(!this._events[event])
            return
        delete this._events[event];
    }

}
const eventEmitter = new EventEmitter();

export default eventEmitter;